package com.yxw.live_vod_boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.live_vod_boot.dto.live_comment.*;
import com.yxw.live_vod_boot.entity.YxFollowEntity;
import com.yxw.live_vod_boot.entity.YxLiveCommentEntity;
import com.yxw.live_vod_boot.entity.YxSysUserEntity;
import com.yxw.live_vod_boot.enums.MqttTopicEnum;
import com.yxw.live_vod_boot.mapper.*;
import com.yxw.live_vod_boot.mqtt.MqttGateway;
import com.yxw.live_vod_boot.service.LiveCommentService;
import com.yxw.live_vod_boot.service.LiveLikeService;
import com.yxw.live_vod_boot.utils.AssertUtil;
import com.yxw.live_vod_boot.utils.JsonUtils;
import com.yxw.live_vod_boot.utils.PageUtils;
import com.yxw.live_vod_boot.vo.*;
//import com.yxw.live_vod_boot.vo.OnDemandCommentVO;
import com.yxw.yxnet_cd_center.common.dto.PageVO;

import com.yxw.yxnet_cd_center.common.enums.ErrorCodeEnum;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class LiveCommentServiceImpl extends ServiceImpl<LiveCommentMapper, YxLiveCommentEntity> implements LiveCommentService {

    @Autowired
    private LiveCommentMapper liveCommentMapper;

    @Autowired
    private YxLiveAddressMapper oYxLiveAddressMapper;

    @Autowired
    private MqttGateway mqttGateway;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private FollowMapper followMapper;

    @Autowired
    private LiveLikeService liveLikeService;

    /**
     * 新增直播评论
     */
    @Override
    @Transactional
    public LiveCommentVO addComment(LiveCommentDTO dto) {
        // 校验当前评论人是否被该直播间禁言
        Boolean isLimit = liveCommentMapper.checkCommentUserIsLimit(dto.getLiveId(), dto.getUserId());
        AssertUtil.isFalse(isLimit, ErrorCodeEnum.COMMENT_USER_IS_LIMIT);
        YxLiveCommentEntity entity = new YxLiveCommentEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setLiveId(dto.getLiveId());
        entity.setUserId(dto.getUserId());
        entity.setUserName(dto.getUserName());
        entity.setToUserId(dto.getToUserId());
        entity.setToUserName(dto.getToUserName());
        entity.setComment(dto.getComment());
        entity.setCommentType(dto.getCommentType());
        entity.setCommentTime(new Date());
        baseMapper.insert(entity);
        LiveCommentVO vo = new LiveCommentVO();
        YxSysUserEntity yxSysUserEntity = userMapper.selectById(vo.getUserId());
        if (null != yxSysUserEntity && null != yxSysUserEntity.getHeadUrl()){
            vo.setHeadUrl(yxSysUserEntity.getHeadUrl());
        }
        BeanUtils.copyProperties(entity, vo);
        // 发送评论消息
        sendLiveCommentMsg(dto, vo);
        return vo;
    }

    private void sendLiveCommentMsg(LiveCommentDTO dto, LiveCommentVO vo) {
        MqttLiveCommentVo mqttVo = new MqttLiveCommentVo();
        mqttVo.setType(1);
        // 接收人ID
        mqttVo.setReceiverId(dto.getLiveId());
        mqttVo.setSysTime(new Date());
        // 内容
        mqttVo.setContent(vo);
        log.info("推送直播ID=============================================>{}", dto.getLiveId());
        log.info("推送评论内容=============================================>{}", dto.getComment());
        mqttGateway.sendToMqtt(MqttTopicEnum.LIVE_COMMENT_TOPIC.getTopic() + dto.getLiveId(), JsonUtils.toJson(mqttVo));
    }

    /**
     * 查询某一直播间下的评论列表
     */
    @Override
    public PageVO<LiveCommentVO> getLiveCommentLisByLiveId(LiveCommentQueryDTO dto) {
        Page<LiveCommentVO> page = new Page<>(dto.getCurrPage(), dto.getPageSize());
        List<LiveCommentVO> list = liveCommentMapper.getLiveCommentLisByLiveId(page, dto.getLiveId());
        return PageUtils.get(page, list);
    }

    /**
     * 查询某一直播间下的评论总数和评论人数量
     */
    @Override
    public LiveCommentStatsVO getCommentStatsByLiveId(LiveCommentQueryDTO dto) {
        return liveCommentMapper.getCommentStatsByLiveId(dto.getLiveId());
    }

    /**
     * 禁言直播间中某一个用户
     */
    @Override
    @Transactional
    public void silencedUser(LiveCommentSilencedUserDTO dto) {
        // 校验当前用户是不是已经被禁言了
        Boolean isLimit = liveCommentMapper.checkCommentUserIsLimit(dto.getLiveId(), dto.getUserId());
        AssertUtil.isFalse(isLimit, ErrorCodeEnum.ADMIN_COMMENT_USER_IS_LIMIT);
        // 禁言
        liveCommentMapper.silencedUser(dto);
    }

    /**
     * 查询直播间中被禁言用户列表
     */
    @Override
    public PageVO<LiveSilencedUserVO> getSilencedUserList(LiveCommentSilencedUserQueryDTO dto) {
        Page<LiveSilencedUserVO> page = new Page<>(dto.getCurrPage(), dto.getPageSize());
        List<LiveSilencedUserVO> list = liveCommentMapper.getSilencedUserList(page, dto);
        return PageUtils.get(page, list);
    }

    /**
     * 拉黑直播间中某一个用户
     */
    @Override
    @Transactional
    public void bannedUser(LiveBannedUserDTO dto) {
        // 校验当前用户是不是已经被禁言了
        Boolean isLimit = liveCommentMapper.checkLiveUserIsBanned(dto.getLiveId(), dto.getUserId());
        AssertUtil.isFalse(isLimit, ErrorCodeEnum.ADMIN_LIVE_USER_IS_BANNED);
        // 拉黑
        liveCommentMapper.bannedUser(dto);
    }

    /**
     * 查询直播间中被拉黑用户列表
     */
    @Override
    public PageVO<LiveBannedUserVO> getBannedUserList(LiveBannedUserQueryDTO dto) {
        Page<LiveBannedUserVO> page = new Page<>(dto.getCurrPage(), dto.getPageSize());
        List<LiveBannedUserVO> list = liveCommentMapper.getBannedUserList(page, dto);
        return PageUtils.get(page, list);
    }

    /**
     * 解除禁言
     */
    @Override
    @Transactional
    public void deleteSilencedUser(LiveCommentSilencedUserDeleteDTO dto) {
        // 校验当前用户是不是已经被禁言了
        Boolean isLimit = liveCommentMapper.checkCommentUserIsLimit(dto.getLiveId(), dto.getUserId());
        AssertUtil.isTrue(isLimit, ErrorCodeEnum.ADMIN_COMMENT_USER_IS_NOT_LIMIT);
        // 禁言
        liveCommentMapper.deleteSilencedUser(dto);
    }

    /**
     * 解除拉黑
     */
    @Override
    @Transactional
    public void deleteBannedUser(LiveBannedUserDeleteDTO dto) {
        // 校验当前用户是不是已经被拉黑了
        Boolean isLimit = liveCommentMapper.checkLiveUserIsBanned(dto.getLiveId(), dto.getUserId());
        AssertUtil.isTrue(isLimit, ErrorCodeEnum.ADMIN_LIVE_USER_IS_NOT_BANNED);
        // 拉黑
        liveCommentMapper.deleteBannedUser(dto);
    }

    /**
     * 直播结束后获取互动数据
     */
    @Override
    public LiveInteractiveDataVO getLiveInteractiveDataAfterLiveEnd(LiveInteractiveDataQueryDTO dto) {
        LiveInteractiveDataVO dataVO = new LiveInteractiveDataVO();
        LambdaQueryWrapper<YxFollowEntity> followWrapper = new LambdaQueryWrapper<>();
        followWrapper.eq(YxFollowEntity::getFollowScene, 1)
                .eq(YxFollowEntity :: getFolloweeShopId, dto.getShopId())
                .ge(YxFollowEntity::getFollowTime, dto.getLiveStartTime())  // 大于等于直播开始时间
                .le(YxFollowEntity::getFollowTime, dto.getLiveEndTime());  // 小于等于直播结束时间
        List<YxFollowEntity> followList = followMapper.selectList(followWrapper);
        // 统计新增关注数量
        dataVO.setAddedFansNumber((long) followList.size());
        LiveCommentStatsVO statsVO = liveCommentMapper.getCommentStatsByLiveId(dto.getLiveId());
        // 统计评论数量和评论人数量
        dataVO.setLiveCommentNumber(statsVO.getTotalCommentCount());
        dataVO.setLiveCommentUserNumber(statsVO.getTotalUserCount());
        // 统计点赞数量
        BaseResult liveLikeInfo = liveLikeService.getLiveLikeInfo(dto.getLiveId());
        LiveLikeInfoVo data = (LiveLikeInfoVo) liveLikeInfo.getData();
        dataVO.setLiveLikeNumber(data.getLikeCnt());
        return dataVO;
    }
}